﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace FinPlusCompView
{
    public static class DataGridExtension
    {
        public static DataGridCell GetCell(this DataGrid dataGrid, DataGridRow dataGridRow, int column)
        {
            if (dataGridRow != null)
            {
                var presenter = dataGridRow.GetVisualChild<DataGridCellsPresenter>();

                if (presenter == null)
                {
                    dataGrid.ApplyTemplate();
                    presenter = dataGridRow.GetVisualChild<DataGridCellsPresenter>();
                }
                
                if (presenter != null)
                {
                    var cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                    if (cell == null)
                    {
                        dataGrid.ScrollIntoView(dataGridRow, dataGrid.Columns[column]);
                        cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
                    }
                    return cell;
                }
            }
            return null;
        }

        public static DataGridCell GetCell(this DataGrid dataGrid, int row, int column)
        {
            var dataGridRow = dataGrid.GetRow(row);
            return dataGrid.GetCell(dataGridRow, column);
        }

        public static DataGridRow GetRow(this DataGrid dataGrid, int index)
        {
            var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                dataGrid.UpdateLayout();
                dataGrid.ScrollIntoView(dataGrid.Items[index]);
                row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }

        public static DataGridRow GetSelectedRow(this DataGrid dataGrid)
        {
            return (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem);
        }

        public static double GetValue(this DataGridCellInfo item)
        {
            var val = 0.0;

            var cellContent = item.Column.GetCellContent(item.Item);
            if (cellContent == null) 
                return val;

            var textBlock = cellContent.FindChild<TextBlock>();
            if (textBlock != null && textBlock.Text != null)
            {
                double.TryParse(textBlock.Text, out val);
                return val;
            }

            var textBox = cellContent.FindChild<TextBox>();
            if (textBox != null && textBox.Text != null)
            {
                double.TryParse(textBox.Text, out val);
                return val;
            }

            return val;
        }

        public static FinPlusControl GetControl(this DataGrid dataGrid, DataGridCellInfo item)
        {
            var cellContent = item.Column.GetCellContent(item.Item);
            if (cellContent == null)
                return null;

            return cellContent.FindChild<FinPlusControl>();
        }
    }
}
